package com.service;

import com.dao.CommentDao;
import com.dto.CommentDTO;
import com.dto.ReaderCommentDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.util.RestException;
import com.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

/**
 * @author linyukun
 * @date 2022/10/18 15:57
 */
@Service
public class CommentServiceImpl {

    @Autowired
    private CommentDao commentDao;

    public Object getCommentList(CommentDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo(commentDao.getCommentList(dto));
    }

    public Integer addComment(CommentDTO dto) {
        Assert.notNull(dto.getBookId(),"请传递书籍id");
        Assert.notNull(dto.getContent(),"请填写评论");
        return commentDao.addComment(dto);
    }

    public Object delComment(CommentDTO dto) {
        Assert.notNull(dto.getId(),"请传递id");
        return commentDao.delComment(dto);
    }

    public Object auditComment(CommentDTO dto) {
        Assert.notNull(dto.getId(),"请传递id");
        CommentVO c = commentDao.getCommentById(dto.getId());
        if(ObjectUtils.isEmpty(c)){
            throw new RestException("找不到对象");
        }
        if(c.getState().intValue() != 0 ){
            throw new RestException("该条数据已经审核了");
        }
        return commentDao.auditComment(dto);
    }
}
